var Barrage = (function () {
    function Barrage(timer, text) {
        if (text === void 0) { text = "未命名"; }
        this.timer = timer;
        this.text = text;
        if (timer) {
            timer.add(this.tick.bind(this));
            this.el = document.createElement("div");
            this.el.className = "b-block";
            this.el.textContent = text;
            document.body.appendChild(this.el);
            this.position = {
                left: window.innerWidth - this.el.clientWidth,
                top: (window.innerHeight - Math.random() * 100 - this.el.clientHeight)
            };
            this.speed = 80 + Math.random() * 3;
            this.un_speed = 0;
            this.lastTime = +new Date();
            this.bindEvent();
            this.setPos();
        }
    }
    Barrage.prototype.bindEvent = function () {
        var _this = this;
        this.el.addEventListener("mouseenter", function () {
            _this.state = 1;
            console.log("over");
            _this.stateChangeTime = +new Date();
        }, false);
        this.el.addEventListener("mouseleave", function (ev) {
            _this.state = 2;
            _this.stateChangeTime = +new Date();
        }, false);
    };
    Barrage.prototype.tick = function (currTime) {
        console.log("tick");
        var duration = (currTime - this.lastTime) / 1000;
        if (this.state == 1) {
            this.un_speed = this.easeIn(0, this.speed, (currTime - this.stateChangeTime) / 1000, .3);
            if (this.un_speed >= this.speed) {
                this.un_speed = this.speed;
                this.state = 0;
            }
        }
        else if (this.state == 2) {
            this.un_speed = this.speed - this.easeIn(0, this.speed, (currTime - this.stateChangeTime) / 1000, .3);
            if (this.un_speed <= 0) {
                this.un_speed = 0;
                this.state = 0;
            }
        }
        var distance = duration * (this.speed - this.un_speed);
        this.position.left -= distance;
        this.setPos();
        this.lastTime = +new Date();
        if (this.position.left < 0 - this.el.clientWidth) {
            document.body.removeChild(this.el);
            return false;
        }
        return true;
    };
    Barrage.prototype.setPos = function () {
        this.el.style.left = this.position.left + "px";
        this.el.style.top = this.position.top + "px";
    };
    Barrage.prototype.easeIn = function (start, alter, curTime, dur) {
        return start + Math.pow(curTime / dur, 2) * alter;
    };
    return Barrage;
}());
